//
// Created by jmdvirus on 2019/12/31 下午12:24.
// Copyright (c) 2019 benewtech All rights reserved.
//     
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//   *  //   *  *  *   *\ / ||  |  |    |
//    **    *  *  *---* *  *   *--* *--*
// 

#include "OpenGLQtSimple.h"
#include "OpenGLQtWidget.h"
#include "cm_logprint.h"

OpenGLQtSimple::OpenGLQtSimple(QWidget *parent) :QWidget(parent) {
    int x = 2;
    if (x == 1) {
        m_qtEasy = new OpenGLQtEasy(480, 320, this);
        m_qtWidget = NULL;
    } else if (x == 2) {
        m_qtWidget = new OpenGLQtWidget(this);
        m_qtEasy = NULL;
    }
    m_stop = false;
}

void *qt_simple_thread(void *args) {
    OpenGLQtSimple *s = (OpenGLQtSimple*)args;
    s->runReadFile();
    s->runImageFromCapture();
    s->runShowImage();
    return NULL;
}

int OpenGLQtSimple::init(int width, int height) {
#ifdef WITH_OPENCV
    m_capture.open(0);
#endif
    m_winWidth = width;
    m_winHeight = height;
    resize(m_winWidth, m_winHeight);
    show();
    if (m_qtWidget) {
        m_qtWidget->setGeometry(0, 0, m_winWidth, m_winHeight);
    } else if (m_qtEasy) {
        m_qtEasy->show();
    }
    pthread_t pid;
    pthread_create(&pid, NULL, qt_simple_thread, this);
    return 0;
}

int OpenGLQtSimple::runImageFromCapture() {
#ifdef WITH_OPENCV
    if (!m_qtEasy && !m_capture.opened()) return -1;

    cminfo("run image from capture\n");
    QImage image;

    while (!m_stop) {
        if (m_capture.getImage(image)) {
            m_qtEasy->renderDisplay(image);
        }
        usleep(40000);
    }
#endif
    return 0;
}

int OpenGLQtSimple::runShowImage() {
    if (!m_qtEasy) return -1;

    char *imgfile[2] = {"/opt/data/testav/1.jpg", "/opt/data/testav/3.jpg"};
    QImage image;
    int ind = 0;

    while (!m_stop) {
        image = QImage(imgfile[ind++]);
        if (ind > 1) ind = 0;
        m_qtEasy->renderDisplay(image);
        sleep(1);
    }
    return 0;
}

int OpenGLQtSimple::runReadFile() {
    if (!m_qtWidget) return -1;
    //Open YUV420P file
#ifdef _WIN32
    fopen_s(&m_file, filename, "rb");
#else
    FILE *m_file = fopen(m_yuvFilename.c_str(), "rb");
#endif
    if (m_file == NULL) {
        LOG("cannot open this file [%s]\n", m_yuvFilename.c_str());
        return -1;
    }

    int m_width = m_yuvWidth;
    int m_height = m_yuvHeight;
    int size = m_yuvWidth * m_yuvHeight * 3 / 2;
    unsigned char *buf = new unsigned char[size];
    while(!m_stop)
    {
        if (fread(buf, 1, m_width*m_height * 3 / 2, m_file) != m_width*m_height * 3 / 2) {
            // Loop
            fseek(m_file, 0, SEEK_SET);
            fread(buf, 1, m_width*m_height * 3 / 2, m_file);
        }

        m_qtWidget->set_data(buf, m_width, m_height);

        sys_sleep(40);
    }

    return 0;
}

int OpenGLQtSimple::setFile(int width, int height, const char *filename) {
    m_yuvFilename = filename;
    m_yuvWidth = width;
    m_yuvHeight = height;
    return 0;
}
